<?php

require_once($controllerDir . '/pclinkhash.php');
require_once($modelDir . '/loginmodel.php');
require_once($controllerDir . '/logincontroller.php');

class MainController {

  protected $linkController;
  protected $loginController;

  public function __construct() {
    $this->linkController = PCLinkHash::getInstance();
    $this->loginController = new LoginController(new loginmodel());
  }
        
  public function invoke() {
//echo "MainController::invoke(): " . $_GET['mkey'] . " : " . $this->linkController->getPageHash($this->loginController->getActionControllerName());
    if($_GET['mkey'] == $this->linkController->getPageHash($this->loginController->getActionControllerName()))
    {
        echo "Получил имя: " . htmlspecialchars($_POST['myusername']);
        echo "Получил пароль: " . htmlspecialchars($_POST['mypassword']);
      $this->loginController->getLoginModel()->setUser(htmlspecialchars($_POST['myusername']));
      $this->loginController->getLoginModel()->setPassword(htmlspecialchars($_POST['mypassword']));
      if($this->isOpenSession() == false) {
  //       echo "session is not opened";
         $this->loginController->invoke();
       }
    }
    else {
  //     echo "session dispatcher called__";
       $this->sessionDispatcher();
    }

/*
   $oFriendList = $this->oFriendList;
                if($_GET['mkey'] == $this->startkey) {
                  $aFriend = $oFriendList->fetch();
                  include $baseDir . '/lib/view/friendlist.php';
                }
		if(isset($_GET['mkey'])) {
			$oFriendList->setKey($_GET['mkey']);
			$oFriend = $oFriendList->fetch();
			include $baseDir . '/lib/view/friendone.php';
		}else {
			$aFriend = $oFriendList->fetch();
			include $baseDir . '/lib/view/friendlist.php';
		}
*/
  }

  protected function isOpenSession() {
    if($this->loginController->getSessionState() != OPENED_SESSION)
      return False;
    else
     return True;
  }


  protected function openedSession() {
//    $this->isOpenSession = True;
//    $this->openCabinetMainPage();
  }

  protected function openCabinetMainPage() {
  }

  protected function sessionDispatcher() {
   if($this->isOpenSession() == False) {
     $this->loginController->invoke();
   }
  }
}

?>
